package com.fht.service.food.impl;

import com.fht.entity.FoodMaterialCategory;
import com.fht.entity.FoodTasteCategory;
import com.fht.enums.ManagermentSystemEnum;
import com.fht.exception.ManagermentSystemException;
import com.fht.mapper.food.FoodMaterialCategoryMapper;
import com.fht.service.food.FoodMaterialCategoryService;
import com.fht.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class FoodMaterialCategoryServiceImpl implements FoodMaterialCategoryService {

    @Resource
    public FoodMaterialCategoryMapper foodMaterialCategoryMapper;


    @Override
    public List<FoodMaterialCategory> selectBymerchantsId(String merchantsId) {
        Example example = new Example(FoodMaterialCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("merchantsId",merchantsId);
        List<FoodMaterialCategory> categorys = foodMaterialCategoryMapper.selectByExample(example);

        return categorys;

    }

    @Override
    public List<FoodMaterialCategory> selectAllUnit() {
        Example example = new Example(FoodMaterialCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("merchantsId","0");
        List<FoodMaterialCategory> units = foodMaterialCategoryMapper.selectByExample(example);
        return units;
    }

    @Override
    @Transactional
    public void addCategory(FoodMaterialCategory foodMaterialCategory) {
        Example example = new Example(FoodMaterialCategory.class);
        Example.Criteria c = example.createCriteria();
        if ("".equals(foodMaterialCategory.getMerchantsId()) || null == foodMaterialCategory.getMerchantsId()){

            c.andEqualTo("merchantsId", "0");
            c.andEqualTo("unit", foodMaterialCategory.getUnit());
            List<FoodMaterialCategory> foodMaterialCategories = foodMaterialCategoryMapper.selectByExample(example);
            if (foodMaterialCategories.size() > 0) {
                throw new ManagermentSystemException(ManagermentSystemEnum.FOOD_CATEGORY_ADD_REPETITION);
            }
            foodMaterialCategory.setMerchantsId(Integer.valueOf("0"));
        }else {


            c.andEqualTo("merchantsId", foodMaterialCategory.getMerchantsId());
            c.andEqualTo("categoryName", foodMaterialCategory.getCategoryName());
            List<FoodMaterialCategory> foodMaterialCategories = foodMaterialCategoryMapper.selectByExample(example);
            if (foodMaterialCategories.size() > 0) {
                throw new ManagermentSystemException(ManagermentSystemEnum.FOOD_CATEGORY_ADD_REPETITION);
            }
        }

        foodMaterialCategoryMapper.insertSelective(foodMaterialCategory);

    }

    @Override
    public void update(FoodMaterialCategory foodMaterialCategory) {


        foodMaterialCategoryMapper.updateByPrimaryKeySelective(foodMaterialCategory);

    }

    @Override
    public void delelteById(String id) {
        foodMaterialCategoryMapper.deleteByPrimaryKey(id);
    }


}
